Attend the Innovation Revolution - Nov 16th
devtools::install_github('rstudio/tensorflow')
devtools::install_github('rstudio/keras')
devtools::install_github('rstudio/tfdeploy')
devtools::install_github('rstudio/cloudml')
devtools::install_github('rstudio/rsconnect')
# Run Python inside R - surprisingly fast!
# Also translates numpy dataframes to R dataframes on the fly 😮
devtools::install_github('rstudio/reticulate') ## [1] '1.9'
sess <- tf$Session()
data <- tf$
contrib$
learn$
datasets$
mnist$
read_data_sets('MNIST', one_hot = T)
len <- length(data$test$images[1,])[[1]]
len## [1] 784
correct_prediction <- tf$equal(tf$argmax(Y, 1L), tf$argmax(Y_, 1L))
accuracy <- tf$reduce_mean(tf$cast(correct_prediction, tf$float32))
sess$run(accuracy, feed_dict=dict(input = data$test$images, Y_ = data$test$labels))## [1] 0.9179
## [1] TRUE
library(zeallot)
# load data
c(c(x_train, y_train), c(x_test, y_test)) %<-% dataset_mnist()
# reshape and rescale
x_train <- array_reshape(x_train, dim = c(nrow(x_train), 784)) / 255
x_test <- array_reshape(x_test, dim = c(nrow(x_test), 784)) / 255
# one-hot encode response
y_train <- to_categorical(y_train, 10)
y_test <- to_categorical(y_test, 10)# define and compile model
model <- keras_model_sequential()
model %>%
layer_dense(units = 32, activation = 'relu', input_shape = c(784),
name = "image") %>%
layer_dense(units = 16, activation = 'relu') %>%
layer_dense(units = 10, activation = 'softmax',
name = "prediction") %>%
compile(
loss = 'categorical_crossentropy',
optimizer = optimizer_rmsprop(),
metrics = c('accuracy')
)Enjoy the show
https://www.tensorflow.org/lite/